package chapter4.No4_1_使用reentrantLock类.demo15;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2016/11/22.
 */
public class ServiceClass3 {
    ReentrantLock lock = new ReentrantLock();

    public void methodA() {

        try {
            //        if(lock.tryLock()) {
            if(lock.tryLock(3, TimeUnit.SECONDS)) {
                System.out.println(Thread.currentThread().getName()+"获取锁"+System.currentTimeMillis());
                Thread.sleep(10000);
            }else {
                System.out.println(Thread.currentThread().getName()+"没有获取锁"+System.currentTimeMillis());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

}
